package it.okkam.geosparql;

import it.okkam.strutturedati.ExtentCcc;
import it.okkam.utility.UtilityQuery;

public class GeoStringQuery {
	public String getStringQueryExtentComuni() {
		
		String queryString = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> ";
		queryString=queryString+" PREFIX pc: <http://www.okkam.it/ont/particella_catastale.owl#>";
		queryString=queryString+" SELECT ?comune ?codiceComune ?extentMin ?extentMax ?extentMinLat ?extentMinLong ?extentMaxLat ?extentMaxLong ";
		queryString=queryString+" FROM <http://www.okkam.org/provinciale/extents> ";
		queryString=queryString+" WHERE { ?comune a pc:Comune . " ;
		queryString=queryString+"?comune pc:haCodiceComuneCatastale ?codiceComune . ";
		queryString=queryString+"?comune pc:haExtentMin ?extentMin . ";
		queryString=queryString+"?comune pc:haExtentMax ?extentMax . ";
		queryString=queryString+"?extentMin geo:lat ?extentMinLat . ";
		queryString=queryString+"?extentMin geo:long ?extentMinLong . ";
		queryString=queryString+"?extentMax geo:lat ?extentMaxLat . ";
		queryString=queryString+"?extentMax geo:long ?extentMaxLong  ";

		queryString=queryString+"}";
		return queryString;
	}
	
	public String getStringQueryExtentParticelle() {
		
		String queryString = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> ";
		queryString=queryString+" PREFIX pc: <http://www.okkam.it/ont/particella_catastale.owl#>";
		queryString=queryString+" SELECT ?comune ?codiceComune ?extentMin ?extentMax ?extentMinLat ?extentMinLong ?extentMaxLat ?extentMaxLong ";
		queryString=queryString+" FROM <http://www.okkam.org/provinciale/extents> ";
		queryString=queryString+" WHERE { ?comune a pc:Comune . " ;
		queryString=queryString+"?comune pc:haCodiceComuneCatastale ?codiceComune . ";
		queryString=queryString+"?comune pc:haExtentMin ?extentMin . ";
		queryString=queryString+"?comune pc:haExtentMax ?extentMax . ";
		queryString=queryString+"?extentMin geo:lat ?extentMinLat . ";
		queryString=queryString+"?extentMin geo:long ?extentMinLong . ";
		queryString=queryString+"?extentMax geo:lat ?extentMaxLat . ";
		queryString=queryString+"?extentMax geo:long ?extentMaxLong  ";

		queryString=queryString+"}";
		return queryString;
	}
	

	 /**
	 * Il metodo restituisce la stringa della query per ottenere i punti contenuti nell extent.
	 * 
	 * @param  String extent
	 * @return String query
	 */


	public String getStringPointsInExtent(String extent) {
		// TODO Auto-generated method stub
		String queryString = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> ";
		queryString=queryString+" PREFIX gs: <java:org.geospatialweb.arqext.> ";
		queryString=queryString+" PREFIX pc: <http://www.okkam.it/ont/particella_catastale.owl#>";
		queryString=queryString+" SELECT  ?id   ";
		queryString=queryString+" FROM <http://www.okkam.org/provinciale/cambiamenti> ";
		
		queryString=queryString+" WHERE {  ";
		
		queryString=queryString+" ?punto gs:within( ";
		queryString=queryString+extent+ ") . " ;
		queryString=queryString+ "?punto pc:puntoDi ?vertice . ";
		queryString=queryString+ "?vertice pc:verticeDi ?poligono .  ";
		queryString=queryString+ "?poligono pc:poligonoDi ?cambiamento . ";
		queryString=queryString+ "?cambiamento pc:haID ?id ";


		queryString=queryString+"}";
		
		return queryString;
	}
	
	public String getQSgetGeometria(String id) {
		// TODO Auto-generated method stub
		String queryString = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> ";
		queryString=queryString+" PREFIX gs: <java:org.geospatialweb.arqext.> ";
		queryString=queryString+" PREFIX pc: <http://www.okkam.it/ont/particella_catastale.owl#>";
		queryString=queryString+" SELECT  ?lat ?long   ";
		queryString=queryString+" FROM <http://www.okkam.org/provinciale/cambiamenti>";
		queryString=queryString+" WHERE {  ";
		queryString=queryString+ "?cambiamento pc:haID ?id . ";
		queryString=queryString+ "?cambiamento pc:haPoligono ?poligono . ";
		queryString=queryString+ "?poligono pc:haVertice ?vertice  .  ";
		queryString=queryString+ "?vertice pc:haPunto ?punto . ";
		queryString=queryString+ "?punto geo:lat ?lat . ";
		queryString=queryString+ "?punto geo:long ?long . ";
		queryString=queryString+ " FILTER (?id = \""+ id +"\")";
		queryString=queryString+"}";
		return queryString;
	}
	public String prova() {//DA finire
		// TODO Auto-generated method stub
		
		String stringQuery = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> ";
		stringQuery = stringQuery +" PREFIX pc: <http://www.okkam.it/ont/particella_catastale.owl#> ";
		stringQuery = stringQuery +" PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#> ";
		stringQuery = stringQuery +" SELECT ?numero ?tipo ?particella   ?extentMinLat ?extentMinLong ?extentMaxLat ?extentMaxLong  ?poligono";
		stringQuery = stringQuery +" FROM <http://www.okkam.org/giovo/particelle> ";  
		stringQuery = stringQuery +" WHERE {" ;
		stringQuery = stringQuery +" ?particella a pc:Particella . ";  
		stringQuery = stringQuery +"	?particella pc:haExtentMin ?extentMin . ";  
		stringQuery = stringQuery +"	?particella pc:haExtentMax ?extentMax . ";  
		stringQuery = stringQuery +"	?extentMin geo:lat ?extentMinLat . ";  
		stringQuery = stringQuery +"	?extentMin geo:long ?extentMinLong . ";  
		stringQuery = stringQuery +"	?extentMax geo:lat ?extentMaxLat . ";  
		stringQuery = stringQuery +"	?extentMax geo:long ?extentMaxLong . ";  
		stringQuery = stringQuery +"	?particella pc:haNumero ?numero . ";  
		stringQuery = stringQuery +"	?particella pc:haPoligono ?poligono .";  
		stringQuery = stringQuery +"	?poligono pc:haVertice ?vertice . ";  
		stringQuery = stringQuery +"	?vertice pc:haPunto ?punto . ";  
		stringQuery = stringQuery +"	?vertice pc:haNumeroOrdine ?ordine . ";  
		stringQuery = stringQuery +"	FILTER (?extentMinLat = 3.3^^<http://www.w3.org/2001/XMLSchema#double> ) ";  
		stringQuery = stringQuery +"	} ";  
		stringQuery = stringQuery +"	ORDER BY xsd:decimal(?ordine)";  
		stringQuery = stringQuery +"}";
	
		return stringQuery;
	}
	
	public String getStringQueryInfo(ExtentCcc extCcc) {//DA finire
		// TODO Auto-generated method stub
		String ccc = extCcc.getCcc();
		UtilityQuery ut = new UtilityQuery();
		String comune = ut.getComune(ccc);
		String stringQuery = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> ";
		stringQuery = stringQuery +" PREFIX pc: <http://www.okkam.it/ont/particella_catastale.owl#> ";
		stringQuery = stringQuery +" PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#> ";
		stringQuery = stringQuery +" SELECT ?nPert ?ccc ?numero ?tipologia ?edificialeFondiaria ?particella   ?extentMinLat ?extentMinLong ?extentMaxLat ?extentMaxLong  ?poligono ?vertice ?punto ?ordine ?puntoLat ?puntoLong ?denominatore ";
		stringQuery = stringQuery +" FROM <http://www.okkam.org/"+comune+"/particelle> ";  
		stringQuery = stringQuery +" WHERE {" ;

		stringQuery = stringQuery +" ?particella a pc:Particella . ";  
		stringQuery = stringQuery +" ?particella pc:haCodiceComuneCatastale ?ccc . ";
		stringQuery = stringQuery +" ?particella pc:eDiTipo ?tipologia . ";
		stringQuery = stringQuery +" ?particella pc:haTipoRelazione ?edificialeFondiaria . ";
		stringQuery = stringQuery +"	?particella pc:haExtentMin ?extentMin . ";  
		stringQuery = stringQuery +"	?particella pc:haExtentMax ?extentMax . ";  
		stringQuery = stringQuery +"	?extentMin geo:lat ?extentMinLat . ";  
		stringQuery = stringQuery +"	?extentMin geo:long ?extentMinLong . ";  
		stringQuery = stringQuery +"	?extentMax geo:lat ?extentMaxLat . ";  
		stringQuery = stringQuery +"	?extentMax geo:long ?extentMaxLong . ";  
		stringQuery = stringQuery +"	?particella pc:haNumero ?numero . ";  
		stringQuery = stringQuery +"	?particella pc:haPoligono ?poligono .";  
		stringQuery = stringQuery +"	?poligono pc:haVertice ?vertice . ";  
		stringQuery = stringQuery +"	?vertice pc:haPunto ?punto . ";  
		stringQuery = stringQuery +"	?vertice pc:haNumeroOrdine ?ordine . ";  
		stringQuery = stringQuery +"	?punto geo:long ?puntoLong . ";  
		stringQuery = stringQuery +"	?punto geo:lat ?puntoLat . 	 ";  
		stringQuery = stringQuery +"	FILTER (?extentMinLat = \""+Double.toString(extCcc.getExt().xMin)+ "\"^^<http://www.w3.org/2001/XMLSchema#double> &&";
		stringQuery = stringQuery +"	?extentMaxLat = \""+Double.toString(extCcc.getExt().xMax)+ "\"^^<http://www.w3.org/2001/XMLSchema#double> &&";	

		stringQuery = stringQuery +"	?extentMinLong = \""+Double.toString(extCcc.getExt().yMin)+ "\"^^<http://www.w3.org/2001/XMLSchema#double> &&";	

		stringQuery = stringQuery +"	?extentMaxLong = \""+Double.toString(extCcc.getExt().yMax)+ "\"^^<http://www.w3.org/2001/XMLSchema#double> ) ";	
		stringQuery = stringQuery +"	OPTIONAL {?particella pc:haDenominatore ?denominatore . ";
		stringQuery = stringQuery +"	?particella pc:haNumeroPertinenza ?nPert . }";
		stringQuery = stringQuery +"	} ";  
		stringQuery = stringQuery +" GROUP BY  ?nPert ?ccc ?numero ?tipologia ?edificialeFondiaria ?particella   ?extentMinLat ?extentMinLong ?extentMaxLat ?extentMaxLong  ?poligono ?vertice ?punto ?ordine ?puntoLat ?puntoLong ?denominatore";
		
		stringQuery = stringQuery +"	ORDER BY xsd:decimal(?ordine)";  
		//stringQuery = stringQuery +"}";
		

		return stringQuery;
	}
}
